package org.sun.web.controller;

import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.sun.basic.dao.INavigationDao;
import org.sun.basic.dao.ISourceDao;
import org.sun.basic.dao.IUserDao;
import org.sun.basic.model.Navigation;
import org.sun.basic.model.Source;
import org.sun.basic.model.SysUser;
import org.sun.web.util.DataUtil;

@Controller
public class IndexControl {
	
	@Inject
	private IUserDao userDao;
	
	@Inject
	private ISourceDao sourceDao;
	
	@Inject
	private INavigationDao navDao;
	private final Logger logger=LoggerFactory.getLogger(getClass());

	@RequestMapping(value="/test/test",method=RequestMethod.GET)
	public String toTest(){
		logger.debug("------------test--------------");
		return "test/test";
	}
	
	
	/*-------------    table.jsp controller        ---------------------------*/
	
	
	@RequestMapping(value="/index/resource",method=RequestMethod.POST)
	public @ResponseBody String getEditTable(){
		List<Source> srcs=sourceDao.list("from Source");
		logger.debug("------------editTable--------------");
		String src=DataUtil.toJson(srcs);
		return src;
	}
	@RequestMapping(value="/index/resource",method=RequestMethod.GET)
	public String toTable(){
		logger.debug("------------test--------------");
		return "index/resource";
	}
	@RequestMapping(value="/index/save",method=RequestMethod.POST)
	public @ResponseBody String saveTable(@RequestBody List<Source> srcs){
		try {
			for(Source src:srcs){
				sourceDao.add(src);
			}
		} catch (Exception e) {
			logger.error("保存失败！", e);
			return "error";
		}
		return null;
	}
	@RequestMapping(value="/index/delete",method=RequestMethod.POST)
	public @ResponseBody String deleteTable(@RequestBody List<Source> srcs){
		try {
			for(Source src:srcs){
				sourceDao.delete(src.getId());
			}
		} catch (Exception e) {
			logger.error("删除失败！", e);
			return "error";
		}
		return null;
	}
	@RequestMapping(value="/index/update",method=RequestMethod.POST)
	public @ResponseBody String updateTable(@RequestBody List<Source> srcs){
		try {
			for(Source src:srcs){
				sourceDao.update(src);
			}
		} catch (Exception e) {
			logger.error("更新失败！", e);
			return "error";
		}
		return null;
	}
	
	/*-------------    navigation.jsp controller        ---------------------------*/
	
	@RequestMapping(value="/index/navigation",method=RequestMethod.POST)
	public @ResponseBody String getEditNavigation(){
		List<Navigation> navs=navDao.list("from Navigation");
		logger.debug("------------editTable--------------");
		String nav=DataUtil.toJson(navs);
		return nav;
	}
	@RequestMapping(value="/index/navigation",method=RequestMethod.GET)
	public String toNavigation(){
		logger.debug("------------test--------------");
		return "index/navigation";
	}
	@RequestMapping(value="/index/saveNav",method=RequestMethod.POST)
	public @ResponseBody String saveNavigation(@RequestBody List<Navigation> navs){
		try {
			for(Navigation nav:navs){
				navDao.add(nav);
			}
		} catch (Exception e) {
			logger.error("保存失败！", e);
			return "error";
		}
		return null;
	}
	@RequestMapping(value="/index/deleteNav",method=RequestMethod.POST)
	public @ResponseBody String deleteNavigation(@RequestBody List<Navigation> navs){
		try {
			for(Navigation nav:navs){
				navDao.delete(nav.getId());
			}
		} catch (Exception e) {
			logger.error("删除失败！", e);
			return "error";
		}
		return null;
	}
	@RequestMapping(value="/index/updateNav",method=RequestMethod.POST)
	public @ResponseBody String updateNavigation(@RequestBody List<Navigation> navs){
		try {
			for(Navigation nav:navs){
				navDao.update(nav);
			}
		} catch (Exception e) {
			logger.error("更新失败！", e);
			return "error";
		}
		return null;
	}
}
